home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Settle a bet please
- Date: Sat, 30 Mar 96 21:45:49 GMT
- Organization: none
- Message-ID: <828222349snz@genesis.demon.co.uk>
- References: <4jfopb$o9n@news1.sympatico.ca> <KASPER.96Mar29073654@acm.org> <KASPER.96Mar30085147@acm.org>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <KASPER.96Mar30085147@acm.org> kasperowski@acm.org writes:
-
- > | But
- > |
- > | char name[7] = "My name";
- > |
- > | reserves 7 chars, and initializes them to contain
- > |
- > | {'M', 'y', ' ', 'n', 'a', 'm', 'e'}.
- > |
- > | This is perfectly legal C, but the resulting array of characters is
- >
- >In private e-mail, someone said the same thing to me, and I replied,
- >"sure, I guess you're right." I was wrong -- this is illegal.
-
- It is perfectly legal.
-
- >In ANSI C,
- >
- > char name[7] = "My name";
- >
- >which is the same as
- >
- > char name[7] = {'M', 'y', ' ', 'n', 'a', 'm', 'e', '\0'};
-
- No, it is as was originally stated.
-
- >is *not* legal. Section A8.7 on p. 219 of Appendix A of my copy of
- >K&R's _The C Programming Language_, 2nd ed., says:
- >
- > If the array has fixed size, the number of initializers may not
- > exceed the number of members of the array . . .
-
- True.
-
- >The line specifies an array of fixed size 7 chars, and then
- >initializes it with 8 chars. This is illegal.
-
- Not true. This is a special case defined by the language. In that case
- only the 7 explicit characters in the string literal are used to initialise
- the array. ISO 6.5.7:
-
- "An array of character type may be initialized by a character string literal,
- optionally enclosed in braces. Successive characters of the character
- string literal (including the terminating null character if there is room
- or if the array is of unknown size) initialize the elements of the array"
-
- The critical part here is ``if there is room''. In this case there isn't
- so the null character isn't used as an initializer.
-
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-